package com.dap.cms.governance.service.impl;

import com.dap.cms.base.impl.BaseServiceImpl;
import com.dap.cms.governance.common.RequestHolder;
import com.dap.cms.governance.exception.ServiceException;
import com.dap.cms.governance.po.*;
import com.dap.cms.governance.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: t_cms_component表对应的服务接口实现类
 * @author: Fesine
 * @createTime:2018/08/21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CmsComponentServiceImpl extends BaseServiceImpl<CmsComponentPo>
        implements CmsComponentService {
    @Autowired
    private CmsComponentManagerService cmsComponentManagerService;
    @Autowired
    private CmsComponentUserService cmsComponentUserService;
    @Autowired
    private CmsComponentInfoService cmsComponentInfoService;
    @Autowired
    private CmsComponentInfoEnvService cmsComponentInfoEnvService;
    @Autowired
    private CmsMessageService cmsMessageService;
    @Autowired
    private CmsManagerService cmsManagerService;

    @Override
    @Rollback
    public void deleteCmsComponentPo(CmsComponentPo cmsComponentPo) {
        try {
            cmsComponentPo=get(cmsComponentPo);
            delete(cmsComponentPo);
            CmsComponentManagerPo cmsComponentManagerPo = new CmsComponentManagerPo();
            cmsComponentManagerPo.setComponentId(cmsComponentPo.getId());
            cmsComponentManagerService.delete(cmsComponentManagerPo);
            CmsComponentUserPo cmsComponentUserPo = new CmsComponentUserPo();
            cmsComponentUserPo.setComponentId(cmsComponentPo.getId());
            cmsComponentUserService.delete(cmsComponentUserPo);
            CmsComponentInfoPo cmsComponentInfoPo = new CmsComponentInfoPo();
            cmsComponentInfoPo.setComponentId(cmsComponentPo.getId());
            List<CmsComponentInfoPo> cmsComponentInfoPos = cmsComponentInfoService.listAll(cmsComponentInfoPo);
            for (CmsComponentInfoPo componentInfoPo : cmsComponentInfoPos) {
                cmsComponentInfoService.delete(componentInfoPo);
                CmsComponentInfoEnvPo cmsComponentInfoEnvPo = new CmsComponentInfoEnvPo();
                cmsComponentInfoEnvPo.setComponentInfoId(componentInfoPo.getId());
                cmsComponentInfoEnvService.delete(cmsComponentInfoEnvPo);
            }
            CmsManagerPo cmsManagerPo = new CmsManagerPo();
            List<CmsManagerPo> cmsManagerPos = cmsManagerService.listAll(cmsManagerPo);
            for (CmsManagerPo managerPo : cmsManagerPos
                    ) {
                //发消息
                if (managerPo.getId().equals(RequestHolder.getCurrentUser().getId())) {
                    continue;
                }
                cmsMessageService.addMsg(
                        managerPo.getId(),
                        3,
                        "删除组件：" + cmsComponentPo.getComponentName(),
                        "您好，尊敬的用户：\n" + managerPo.getUsername() + ",\n" +
                                RequestHolder.getCurrentUser().getUsername() + "删除了组件" +
                                cmsComponentPo.getComponentName() + "\n" +
                                ",请您知悉！",
                        null
                );
            }
        } catch (Exception e) {
            throw new ServiceException(500, "删除失败");
        }


    }

    @Override
    public List<CmsComponentPo> listByUser(CmsComponentPo cmsComponentPo) {
        return daoService.selectList(CmsComponentPo.class.getName() + ".selectByUserId", cmsComponentPo);

    }

    @Override
    public void add(CmsComponentPo cmsComponentPo) {
        save(cmsComponentPo);
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        List<CmsManagerPo> cmsManagerPos = cmsManagerService.listAll(cmsManagerPo);
        for (CmsManagerPo managerPo : cmsManagerPos
                ) {
            //发消息
            if (managerPo.getId().equals(RequestHolder.getCurrentUser().getId())) {
                continue;
            }
            cmsMessageService.addMsg(
                    managerPo.getId(),
                    3,
                    "添加新组件：" + cmsComponentPo.getComponentName(),
                    "您好，尊敬的用户：\n" + managerPo.getUsername() + ",\n" +
                            RequestHolder.getCurrentUser().getUsername() + "添加了组件" +
                            cmsComponentPo.getComponentName() + "\n" +
                            ",请您知悉！",
                    null
            );
        }
    }
}

